package com.ybwh.springboot2.activiti;

import java.io.InputStream;
import java.util.List;
import java.util.zip.ZipInputStream;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.*;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;

public class AskLeave {
    public static void main(String[] args) {
        ProcessEngine engine = InitTable.init2();

//		deployProcess(engine);
//
//		ProcessInstance processInstance = createProcessInstance(engine);
//		System.out.println(processInstance);

//		findTask(engine);

//		completeTask(engine, "5009");

//        findProccessDefinion(engine);
        findProccessDeploy(engine);


    }


    
    


    /**
     * 查询流程部署
     *
     * @param engine
     */
    public static void findProccessDeploy(ProcessEngine engine) {
        RepositoryService repositoryService = engine.getRepositoryService();
        DeploymentQuery query = repositoryService.createDeploymentQuery();
        query.processDefinitionKey("ask_for_leave");

//        query.deploymentId("5001");
        List<Deployment> list = query.list();
        System.out.println(list);

        // 流程图片
//        InputStream img = repositoryService.getProcessDiagram();

    }

    /**
     * 查询流程定义
     *
     * @param engine
     */
    public static void findProccessDefinion(ProcessEngine engine) {
        RepositoryService repositoryService = engine.getRepositoryService();
        ProcessDefinitionQuery query = repositoryService.createProcessDefinitionQuery();
//        query.processDefinitionKey("ask_for_leave");

        query.deploymentId("5001");
        List<ProcessDefinition> list = query.list();
        System.out.println(list);

        // 流程图片
//        InputStream img = repositoryService.getProcessDiagram();


    }
    
    
    
    /**
     * 获取当前的任务节点
     * 
     * @param proccessId
     * @return
     */
    public  List<Task> getCurrentTask(ProcessEngine engine,String proccessId){
    	TaskService taskService = engine.getTaskService();
    	//这里一般返回一个，有并行网关的话可能返回多个
    	List<Task>  taskList = taskService.createTaskQuery().processInstanceId("流程实例ID").active().list();
    	return taskList;
    }


    /**
     * 完成一个任务节点
     * 
     * @param engine
     * @param taskId
     */
    public static void completeTask(ProcessEngine engine, String taskId) {
        TaskService taskService = engine.getTaskService();
        taskService.complete(taskId);
    }

    
    /**
     * 查找任务节点
     * 
     * @param engine
     */
    public static void findTask(ProcessEngine engine) {
        TaskService taskService = engine.getTaskService();
        List<Task> taskList = taskService.createTaskQuery().taskAssignee("employee").list();
        System.out.println(taskList);
    }

    /**
     * 
     * 创建一个流程实例
     * 
     * @param engine
     * @return
     */
    public static ProcessInstance createProcessInstance(ProcessEngine engine) {
        RuntimeService runtimeService = engine.getRuntimeService();
        //外部业务主键
        String businessKey="325265";
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("ask_for_leave·111",businessKey);
        return processInstance;
    }


    /**
     * 部署流程得到流程部署ID
     *
     * @param engine
     */
    public static void deployProcess(ProcessEngine engine) {
        RepositoryService repositoryService = engine.getRepositoryService();
        DeploymentBuilder builder = repositoryService.createDeployment().name("请假流程")
//                .addZipInputStream(new ZipInputStream(AskLeave.class.getResourceAsStream("/processes/leave.zip")))
        		.addClasspathResource("processes/leave.bpmn")
                .addClasspathResource("processes/leave.bmp")
                ;
        Deployment deploy = builder.deploy();
        System.out.println("部署流程得到流程部署ID:" + deploy.getId());

    }
}
